* This do-file creates tables and figures depicting cropping practices (mono v. mixed/border) by treatment status
* Paper Figure S2 and Table S4


tempfile treats

global B_idi B_y1_e B_y1_s B_y2_e B_y2_s B_y3_e B_y3_s B_y2 B_y3
global B_grp B_y1_t B_y1_t_i B_y2_t B_y2_t_i B_y3_t B_y3_t_i B_y2 B_y3

global estset replace se tex label  mlabels(none) nonotes nonumbers starlevels(* 0.1 ** 0.05 *** 0.01)
global mainstats stats(ctrl_mean r2 N, labels("Control Mean" "R-Squared" "Observations"))  
global mgset prefix(\multicolumn{@span}{c}{) suffix(}) span

*****
* Household treatment assignemnts
*****

use "$out_data/survey_roster.dta", clear
merge m:1 village using "$admin_data/treatment_assignment.dta", keepusing(treat_input treat_ext) assert(match using)
drop if _m==2
drop _m
merge m:1 hhid using "$out_data/survey_drops.dta", assert(match) nogen

// Make year*treat interactions
expand 3
bys hhid: gen byte year = _n
* Dummy out treatment interactions
gen byte B_y2 = year==2
label var B_y2 "Year 2"
gen byte B_y3 = year==3
label var B_y3 "Year 3"
gen byte B_y1_s = (year==1)*(treat_ext==1)
label var B_y1_s "Subs. Yr. 1"
gen byte B_y2_s = (year==2)*(treat_ext==1)
label var B_y2_s "Subs. Yr. 2"
gen byte B_y3_s = (year==3)*(treat_ext==1)
label var B_y3_s "Subs. Yr. 3"
gen byte B_y1_e = (year==1)*(treat_ext==2)
label var B_y1_e "Ext. Yr. 1"
gen byte B_y2_e = (year==2)*(treat_ext==2)
label var B_y2_e "Ext. Yr. 2"
gen byte B_y3_e = (year==3)*(treat_ext==2)
label var B_y3_e "Ext. Yr. 3"
gen byte B_y1_t = (year==1)*(treat_input==1)
label var B_y1_t "Treat Yr. 1"
gen byte B_y2_t = (year==2)*(treat_input==1)
label var B_y2_t "Treat Yr. 2"
gen byte B_y3_t = (year==3)*(treat_input==1)
label var B_y3_t "Treat Yr. 3"
gen byte B_y1_t_i = (year==1)*(treat_input==1)*(bl_group==1)
label var B_y1_t_i "Treat Y1*Farmer Group"
gen byte B_y2_t_i = (year==2)*(treat_input==1)*(bl_group==1)
label var B_y2_t_i "Treat Y2*Farmer Group"
gen byte B_y3_t_i = (year==3)*(treat_input==1)*(bl_group==1)
label var B_y3_t_i "Treat Y3*Farmer Group"

save `treats'

use "$out_data/outcome_patterns.dta", clear
collapse (max) pattern grow_* (sum) area_* frac_*, by(hhid year season) fast
merge m:1 hhid year using `treats', assert(match) nogen

/* Replaced with Figure S2
eststo clear
forvalues s = 1/2 {
foreach vbl in grow_mono grow_mixd {
	eststo: reg `vbl' $B_idi $controls if season==`s' & drop_srvy==0, vce(cluster village)
	quietly sum `vbl' if season==`s' & year==1 & treat_input==0
	estadd scalar ctrl_mean = `r(mean)'
}
}
local col1 "  & Mono & Mixed & Mono & Mixed \\"
local col2 " & (1) & (2) & (3) & (4) \\ \midrule "
local rules \cmidrule(lr){@span}
local est_consume mgroups("Kharif" "Rabi", pattern(1 0 1 0) $mgset  erepeat( `rules')) posthead(`col1' `col2')
esttab using "$out_tables/table_S4B.tex", keep($B_idi) $estset $mainstats `est_consume'
*/
	
eststo clear
forvalues s = 1/2 {
foreach vbl in grow_mono grow_mixd {
	eststo: reg `vbl' $B_grp $controls if season==`s' & drop_srvy==0, vce(cluster village)
	quietly sum `vbl' if season==`s' & year==1 & treat_input==0
	estadd scalar ctrl_mean = `r(mean)'
}
}
local col1 "  & Mono & Mixed & Mono & Mixed \\"
local col2 " & (1) & (2) & (3) & (4) \\ \midrule "
local rules \cmidrule(lr){@span}
local est_consume mgroups("Kharif" "Rabi", pattern(1 0 1 0) $mgset  erepeat( `rules')) posthead(`col1' `col2')
esttab using "$out_tables/Table_S4.tex", keep($B_grp) $estset $mainstats `est_consume'

	
gen byte grow_none = pattern==0
replace grow_mixd=0 if grow_mono==1
//egen test = rowtotal(grow_*)

collapse (sum) grow_*, by(year season treat_ext)

foreach v in mono mixd none {
	gen frac_`v' = grow_`v'/(grow_mono + grow_mixd + grow_none)
}

label define yr 1 "Year 1" 2 "Year 2" 3 "Year 3"
label values year yr

graph set window fontface "CMU Serif"
local grset graphregion(color(white)) ylabel(0(0.2)0.8, angle(0) gmin gmax) plotregion(margin(0))
graph drop _all

graph bar frac_mixd frac_mono if season==1, /// 
	over(treat_ext) over(year) stack `grset' name(kPat) ///
	bar(1, color(gs0)) bar(2,lcolor(gs0) fcolor(gs10)) /// bar(3,lcolor(gs16) fcolor(gs16)) ///
	legend(order(1 "Mixed/Border" 2 "Monocrop")) title("Kharif Crop Patterns")
graph export "$out_figures/Parts/fig_input_patterns_kharif.png", replace
	
graph bar frac_mixd frac_mono if season==2, /// 
	over(treat_ext) over(year) stack `grset' name(rPat)  ///
	bar(1, color(gs0)) bar(2,lcolor(gs0) fcolor(gs10)) /// bar(3,lcolor(gs16) fcolor(gs16)) ///
	legend(order(1 "Mixed/Border" 2 "Monocrop")) title("Rabi Crop Patterns")
graph export "$out_figures/Parts/fig_input_patterns_rabi.png", replace
	 
	
grc1leg kPat rPat, graphregion(color(white)) ycommon 
graph export "$out_figures/Figure_S2.png", replace
